/*
 * dieta_Viewfi_ma.java
 *
 * Created on 26 de mayo de 2008, 23:16
 */
package propres;

import java.util.*;
import javax.swing.DefaultListModel;

/**
 *
 * @author  neochange
 */
public class dieta_Viewfi_ma extends javax.swing.JPanel {

    /** Creates new form dieta_Viewfi_ma */
    public dieta_Viewfi_ma() {
        initComponents();
    }

    public dieta_Viewfi_ma(CtrlVistaCliente CVC, CtrlVistalimento CVA, CtrlVistaPreferencias CVPref, CtrlVistaplato CVP) {
        initComponents();
        CVc = CVC;
        CVa = CVA;
        CVp = CVP;
        CVd = new CtrlVistadieta(CVC, CVA, CVPref, CVp);
        mod = new DefaultListModel();
        mod2 = new DefaultListModel();
        estadolist=0;
        jList1.setModel(mod);
        jList2.setModel(mod2);

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jFrame1 = new javax.swing.JFrame();
        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jButton1 = new javax.swing.JButton();
        jTextField1 = new javax.swing.JTextField();
        jTextField2 = new javax.swing.JTextField();
        jTextField3 = new javax.swing.JTextField();
        jLabel1 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();

        jFrame1.setName("jFrame1"); // NOI18N

        javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
        jFrame1.getContentPane().setLayout(jFrame1Layout);
        jFrame1Layout.setHorizontalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
        );
        jFrame1Layout.setVerticalGroup(
            jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
        );

        setName("Dieta manual"); // NOI18N

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setName("jList1"); // NOI18N
        jList1.addListSelectionListener(new javax.swing.event.ListSelectionListener() {
            public void valueChanged(javax.swing.event.ListSelectionEvent evt) {
                jList1ValueChanged(evt);
            }
        });
        jScrollPane1.setViewportView(jList1);

        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(propres.PropresApp.class).getContext().getResourceMap(dieta_Viewfi_ma.class);
        jButton1.setText(resourceMap.getString("jButton1.text")); // NOI18N
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jTextField1.setText(resourceMap.getString("jTextField1.text")); // NOI18N
        jTextField1.setName("jTextField1"); // NOI18N

        jTextField2.setText(resourceMap.getString("jTextField2.text")); // NOI18N
        jTextField2.setName("jTextField2"); // NOI18N

        jTextField3.setText(resourceMap.getString("jTextField3.text")); // NOI18N
        jTextField3.setName("jTextField3"); // NOI18N

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList2.setName("jList2"); // NOI18N
        jScrollPane2.setViewportView(jList2);

        jButton2.setText(resourceMap.getString("jButton2.text")); // NOI18N
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(resourceMap.getString("jButton3.text")); // NOI18N
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText(resourceMap.getString("jLabel3.text")); // NOI18N
        jLabel3.setName("jLabel3"); // NOI18N

        jCheckBox1.setText(resourceMap.getString("jCheckBox1.text")); // NOI18N
        jCheckBox1.setName("jCheckBox1"); // NOI18N

        jLabel4.setText(resourceMap.getString("jLabel4.text")); // NOI18N
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText(resourceMap.getString("jLabel5.text")); // NOI18N
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText(resourceMap.getString("jLabel6.text")); // NOI18N
        jLabel6.setName("jLabel6"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(48, 48, 48)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addContainerGap())
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jButton1)
                            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGap(25, 25, 25)
                                        .addComponent(jCheckBox1)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 132, Short.MAX_VALUE)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                            .addComponent(jButton3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                            .addComponent(jButton2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
                                        .addGap(53, 53, 53)
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabel3)
                                            .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 183, javax.swing.GroupLayout.PREFERRED_SIZE))))
                                .addGap(89, 89, 89)))
                        .addGap(290, 290, 290))))
            .addGroup(layout.createSequentialGroup()
                .addGap(82, 82, 82)
                .addComponent(jLabel1)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(6, 6, 6)
                        .addComponent(jLabel4)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jLabel6))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, 28, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, 42, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(552, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jLabel6)
                    .addComponent(jLabel4))
                .addGap(6, 6, 6)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jLabel1)
                        .addComponent(jTextField1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(jTextField2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jTextField3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jScrollPane2, 0, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 126, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jButton1)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jButton3))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(27, 27, 27)
                        .addComponent(jCheckBox1)))
                .addGap(69, 69, 69))
        );
    }// </editor-fold>//GEN-END:initComponents
    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // Consultar dia de dieta:
        
        actualizar_list();
        
    }//GEN-LAST:event_jButton1ActionPerformed

    private void jList1ValueChanged(javax.swing.event.ListSelectionEvent evt) {//GEN-FIRST:event_jList1ValueChanged
        // Evento de seleccion de lista

        if( !mod.isEmpty()){
            
            if(jCheckBox1.isSelected()){estadolist = 1;}
            else{estadolist = 2;}

            String t = (String) mod.getElementAt(jList1.getSelectedIndex());

            List<String> F = new ArrayList();

            mod2.clear();

            try {
               
                F = CVd.consplats(estadolist);
                int g = 0;
                while (g < F.size()) {
                    mod2.addElement(F.get(g));
                    g++;
                }
                if (F.isEmpty()) {
                    mod2.addElement("No hay platos en el sistema");
                    //mod.clear();
                }
            } catch (Exception e) {
                System.out.println("Error al generar lista 2");
                Dialog_error u = new Dialog_error(jFrame1, true, e.getMessage());
                u.setLocationRelativeTo(null);
                u.setVisible(true);

            }
        
        }
    }//GEN-LAST:event_jList1ValueChanged

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // Añadir plato seleccionado a una comida de un dia de la dieta

        String[] coms = {"Desayuno:", "Almuerzo:", "Merienda:", "Cena:", "Ressopó:"};

        if (!jList1.isSelectionEmpty() && !jList2.isSelectionEmpty()) {
            String com = (String) mod.getElementAt(jList1.getSelectedIndex());
            boolean b = false;
            int y = 0;
            while (y < coms.length && !b) {
                if (com.equals(coms[y])) {
                    b = true;
                } else {
                    y++;
                }
            }
            if (b) {
                Date h = new Date(Integer.parseInt(jTextField3.getText()), Integer.parseInt(jTextField2.getText()), Integer.parseInt(jTextField1.getText()));
                try {
                    CVd.aplato(y, h, (String) mod2.getElementAt(jList2.getSelectedIndex()),jCheckBox1.isSelected());
                  
                    actualizar_list();
                  
                } catch (Exception e) {
                    System.out.println("Fallo al añadir plato " + e.getMessage());
                }
            } else {
                Dialog_error u = new Dialog_error(jFrame1, true, "Debes seleccionar una comida para añadir plato");
                u.setLocationRelativeTo(null);
                u.setVisible(true);
            }

        }
        jList1.clearSelection();
        jList2.clearSelection();
        jCheckBox1.setSelected(false);
        
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Eliminar plato

        
        
        String[] coms = {"Desayuno:", "Almuerzo:", "Merienda:", "Cena:", "Ressopó:"};
        
        if (!jList1.isSelectionEmpty()) {
           
            String com = (String) mod.getElementAt(jList1.getSelectedIndex());

            boolean b = false;
            int y = jList1.getSelectedIndex();
            int x = 0;
            while (y > 0 && !b) {
                x = 0;
                while (x < 5 && !b) {

                    if (((String) mod.getElementAt(y)).equals(coms[x])) {
                        b = true;
                    } else {
                        x++;
                    }
                }
                if (!b) {
                    y--;
                }
            }

            if (y == 0) {
                x = 0;
                b = true;
            }


           
            if (b) {
                Date h = new Date(Integer.parseInt(jTextField3.getText()), Integer.parseInt(jTextField2.getText()), Integer.parseInt(jTextField1.getText()));
                try {
                
                    CVd.eplato(x, h, com);
                
                    mod.clear();
                    actualizar_list();
                
                    

                } catch (Exception e) {
                    System.out.println("Fallo al eliminar plato " + e.getMessage());
                }
            }


        }
        jList1.clearSelection();
        jList2.clearSelection();
        jCheckBox1.setSelected(false);
        
    }//GEN-LAST:event_jButton3ActionPerformed

    public void actualizar_list() {

        int dia, mes, any;

        if (!jTextField1.getText().equals("") && !jTextField2.getText().equals("") && !jTextField3.getText().equals("")) {
            dia = Integer.parseInt(jTextField1.getText());
            mes = Integer.parseInt(jTextField2.getText());
            any = Integer.parseInt(jTextField3.getText());
            Date fe = new Date(any, mes, dia);

            List<List<String>[][]> U = new ArrayList();

            try {
                U = CVd.cons_dieta(fe, fe);
                
            } catch (Exception e) {
                System.out.println(e.getMessage());
                Dialog_error u = new Dialog_error(jFrame1, true, e.getMessage());
                u.setLocationRelativeTo(null);
                u.setVisible(true);
            }

            mod.clear();
            String[] coms = {"Desayuno:", "Almuerzo:", "Merienda:", "Cena:", "Ressopó:"};
            int i = 0;
            
            while (i < 5) {
                if(i==0){mod.clear();}
                try{
                if(i==0){
                mod = new DefaultListModel();
                jList1.setModel(mod);
                }
                mod.addElement(coms[i]);
                }
                catch(Exception e){
                System.out.println(e.getCause()+" "+e.getMessage()+" "+e.getClass());
                }
                int y = 0;
                
                if (!(U.get(0)[i][0] == null)) {
                    while (y < U.get(0)[i][0].size()) {
                        
                        try{
                        mod.addElement(U.get(0)[i][0].get(y));
                        }
                        catch(Exception e){
                        System.out.println("2- "+e.getCause()+" "+e.getMessage()+" "+e.getClass());
                        }
                       
                        y++;
                    }
                }
                i++;
            }
        } else {
            Dialog_error u = new Dialog_error(jFrame1, true, "Fecha incorrecta o en blanco");
            u.setLocationRelativeTo(null);
            u.setVisible(true);

        }

    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JFrame jFrame1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    private javax.swing.JTextField jTextField2;
    private javax.swing.JTextField jTextField3;
    // End of variables declaration//GEN-END:variables
    private CtrlVistadieta CVd;
    private CtrlVistaCliente CVc;
    private CtrlVistalimento CVa;
    private CtrlVistaplato CVp;
    private DefaultListModel mod;
    private DefaultListModel mod2;
    private int estadolist;
}
